<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Support\MemcacheSASL;

class WechatSet extends BaseModel
{
    protected $table = "wechat_set";
    protected $fillable = ['id', 'name', 'gh_id', 'app_id', 'app_secret', 'token', 'encoding_key'];

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where = [], $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where, $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取商户信息指定缓存
     */
    public function getWechatSet()
    {
        $key = 'yun_wechat_set';
        $cache = $this->memcache->get($key);
        if (!$cache) {
            $data = $this->getOne();
            if($data)
            {
                $data->toArray();
                $this->memcache->set($key, $data);
            }
            return $data;
        }
        return $cache;
    }
}
